﻿using CoreShop.Library.Units;
using CoreShop.Model.FromBody;
using CoreShop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static CoreShop.Services.PosRemoteServices;
using CoreShop.Services;
using CoreShop.Library;
using Sunny.UI.Win32;
using Sunny.UI;
using CoreShop.Model.FromBody.GsDataModel;
using Newtonsoft.Json;
using CoreShop.Model.UI;
using Newtonsoft.Json.Linq;
using static CoreShop.Model.FromBody.DataResultModel;
using System.Collections;

namespace CoreShop.App.Units.Interface
{
    public class MemberApiUtil
    {
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="newPassword"></param>
        /// <param name="confirmPassword"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static async Task<ReturnModel> UpdateCardStatus(corecmsuser member,string canLock, string code)
        {
            try
            {
                JObject keyValues = new JObject();
                keyValues.Add(new JProperty("cardNo", member.gsPosVip.cardNo));
                keyValues.Add(new JProperty("canLock", canLock));
                keyValues.Add(new JProperty("status", member.gsPosVip.status));
                keyValues.Add(new JProperty("phone", member.gsPosVip.vipTel));
                keyValues.Add(new JProperty("code", code));
                ReturnModel result = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.UpdateCardStatus}", keyValues.ToJsonString(), 60, "", AppConstVars.sysUser);
                return result;
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="newPassword"></param>
        /// <param name="confirmPassword"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static async Task<ReturnModel> UpdatePassword(sysuser sysuser, corecmsuser member, string newPassword, string confirmPassword, string code)
        {
            try
            {
                JObject keyValues = new JObject();                
                keyValues.Add(new JProperty("vipId", member.id));//会员id                
                keyValues.Add(new JProperty("vipTel", member.mobile));//电话               
                keyValues.Add(new JProperty("newPassword", newPassword)); //新密码                
                keyValues.Add(new JProperty("confirmPassword", confirmPassword));//新密码                
                keyValues.Add(new JProperty("code", code));//验证码                
                ReturnModel result = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.RestPassword}", keyValues.ToJsonString(), 60, "", AppConstVars.sysUser);
                return result;
            }
            catch { }
            return null;
        }


        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <returns></returns>
        public static async Task<ReturnModel> SendSMSCode(sysuser sysuser, corecmsuser member)
        {
            return await HttpHelperAsync.GetDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.SendSMS}/{member.mobile}", 60, AppConstVars.sysUser);
            //return HttpHelper.GetSendMessage(baseUrl, $"{SERVER.Member}{API.SendSMS}/{member.mobile}", 60, sysuser);
        }


        /// <summary>
        /// 得到会员的券信息
        /// </summary>
        /// <returns></returns>
        public static async Task<ReturnModel> SetCouponInfo(string couponNo, sysuser sysuser)
        {
            try
            {
                string para = $"{SERVER.Coupon}{API.GetCoupon}?couponNo={couponNo}";
                // return HttpHelper.GetSendMessage(baseUrl, para, 60, sysuser);
                return await HttpHelperAsync.GetDataAsync(ApiConsVars.baseUrl, para, 60, AppConstVars.sysUser);
            }
            catch (Exception ex)
            {
                ReturnModel returnModel = new ReturnModel();
                returnModel.Status = ReturnStatus.Error;
                returnModel.MessageOrString = ex.Message;
                return returnModel;
            }
        }


        /// <summary>
        /// 使用券
        /// </summary>
        /// <returns></returns>
        public static async Task<ReturnModel> UseCouponInfo(List<corecmsfoodordering> orderingDetail, CouponInfoDataModel couponInfoData
            , decimal OrginAmt, corecmsstore store, sysuser sysuser, string pwd = "")
        {
            JObject jObject = new JObject();

            //卷码
            jObject.Add(new JProperty("couponNo", couponInfoData.data.couponNo));
            //密码非必须
            jObject.Add(new JProperty("pwd", pwd));
            //门店id
            jObject.Add(new JProperty("branchId", store.branchId));
            //初始金额
            jObject.Add(new JProperty("orderOriginAmount", OrginAmt));
            //渠道
            jObject.Add(new JProperty("couponChannelId", couponInfoData.data.couponChannelId));
            //来源
            jObject.Add(new JProperty("sourceId", couponInfoData.data.couponSourceChannelId));
            //活动id
            jObject.Add(new JProperty("actityId", string.Empty));
            //商品列表
            List<object> list = new List<object>();

            orderingDetail.ForEach(product =>
            {
                JObject valuePairs = new JObject();

                valuePairs.Add(new JProperty("itemCode", product.number));
                valuePairs.Add(new JProperty("amount", product.realAmount));
                list.Add(valuePairs);
            });

            jObject.Add(new JProperty("itemList", list));

            //var result = HttpHelper.PostSendMessage(baseUrl, $"{SERVER.Coupon}{API.ConsumeCoupon}",
            //        jObject.ToJsonString(), 60, "", sysuser);

            ReturnModel result = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Coupon}{API.ConsumeCoupon}", jObject.ToJsonString(), 60, "", AppConstVars.sysUser);
            return result;
        }


        /// <summary>
        /// 获取优惠券
        /// </summary>
        /// <param name="balance"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static async Task<CouponModel> GetCouponByOrder(OrderCouponPost couponPost, string token)
        {
            //try
            //{
            //    msg = "会员消费失败。";
            //    List<corecmsfoodcouponuser> cList = GetCouponListByOrder(couponPost, ref msg, token);
            //    if ((cList != null) && (cList.Count > 0))
            //    {
            //        CouponModel couponModel = new CouponModel();
            //        couponModel.couponUser = cList[0];
            //        return couponModel;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    msg = "系统出错：" + ex.Message;
            //}
            return null;
        }


        /// <summary>
        /// 会员退款
        /// </summary>
        /// <param name="amt"></param>
        /// <param name="businessType"></param>
        /// <param name="mchNo"></param>
        /// <param name="password"></param>
        /// <param name="oriMchNo">原订单号</param>
        /// <returns></returns>
        public static async Task<AdminUiCallBack> VipRefund(long amt, string businessType, string mchNo, string oriMchNo, long branchId)
        {
            JObject keyValues = new JObject();

            //金额
            keyValues.Add(new JProperty("amount", amt));
            //积分类型
            keyValues.Add(new JProperty("billType", 7));
            keyValues.Add(new JProperty("organizationId", 1));
            keyValues.Add(new JProperty("mchNo", mchNo));
            keyValues.Add(new JProperty("branchId", branchId));
            //来源
            keyValues.Add(new JProperty("sourceBillType", 1));
            keyValues.Add(new JProperty("businessType", businessType));
            keyValues.Add(new JProperty("oriMchNo", oriMchNo));
            AdminUiCallBack res = new AdminUiCallBack();
            try
            {
                ReturnModel returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Pay}{API.RefundPayOrder}", JsonConvert.SerializeObject(keyValues), 60, "", AppConstVars.sysUser);
                res = JsonConvert.DeserializeObject<AdminUiCallBack>(returnModel.MessageOrString);
                if (res == null || res.code != 200)
                {
                    res.msg = res.msg;
                    res.code = 1;
                }
                return res;
            }
            catch (Exception ex)
            {
                res.msg = ex.Message;
                res.code = 1;
            }
            return res;
        }



        /// <summary>
        /// 会员付款
        /// </summary>
        /// <param name="vipId"></param>
        /// <param name="amt"></param>
        /// <param name="businessType"></param>
        /// <param name="mchNo"></param>
        /// <param name="password"></param>
        /// <returns></returns>        
        public static async Task<AdminUiCallBack> SendVipPay(VipPayParaDto keyValues)
        {
            AdminUiCallBack res = new AdminUiCallBack();
            try
            {
                ReturnModel returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Pay}{API.CreatePayOrder}", JsonConvert.SerializeObject(keyValues), 60, "", AppConstVars.sysUser);
                res = JsonConvert.DeserializeObject<AdminUiCallBack>(returnModel.MessageOrString);
                if (res == null || res.code != 200)
                {
                    res.msg = res.msg;
                    res.code = 1;
                }
                return res;
            }
            catch (Exception ex)
            {
                res.msg = ex.Message;
                res.code = 1;
            }
            return res;
        }
                

        /// <summary>
        /// 加减积分
        /// </summary>
        /// <param name="balance"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static async Task<ReturnModel> DoUserPoint(corecmsuserpoint userpoint, corecmsuser member)
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                string para = $"{SERVER.Member}{API.SaveVipPointsForPos}/{member.number}/{userpoint.num}/{AppConstVars.sysStore.branchId}";
                returnModel = await HttpHelperAsync.GetDataAsync(ApiConsVars.baseUrl, para, 60, AppConstVars.sysUser);
                //return HttpHelper.GetSendMessage(baseUrl, $"{SERVER.Member}{API.SaveVipPointsForPos}/{member.number}/{userpoint.num}/{store.branchId}", 60, sysuser);
            }
            catch (Exception ex)
            {                
                returnModel.Status = ReturnStatus.Error;
                returnModel.MessageOrString = ex.Message;                
            }
            return returnModel;
        }


        /// <summary>
        /// 会员集会档位
        /// </summary>
        /// <param name="sysuser"></param>
        /// <returns></returns>
        public static async Task<MemberTopUpResult> GetMemberTopUpItem(sysuser sysuser, string vipId)
        {
            MemberTopUpResult result = new MemberTopUpResult();
            try
            {               
                Hashtable ht = new Hashtable();
                JObject keyValues = new JObject();
                keyValues.Add(new JProperty("vipId", vipId));
                ReturnModel returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl,
                    $"{SERVER.Member}{API.PosStandardList}", keyValues.ToJsonString(), 60, "", sysuser);                
                if (returnModel.Status != ReturnStatus.Success)
                {
                    result.msg = $"获取充值档位出错:{returnModel.MessageOrString}";
                    return result;
                }

                TopUpItemMode topUpItemMode = returnModel.MessageOrString.JsonToEntity<TopUpItemMode>();

                if (topUpItemMode.code != 200 || topUpItemMode.data == null)
                {
                    result.msg = $"获取充值档位出错:{topUpItemMode.msg}";
                    return result;
                }
                List<TopUpItemDataInfoMode> RechargeInfo = topUpItemMode.data.ToString().JsonToEntity<List<TopUpItemDataInfoMode>>();
                result.data = RechargeInfo;
                return result;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 修改会员
        /// </summary>
        /// <param name="vipInfo"></param>
        /// <returns></returns>
        public static async Task<ReturnModel> UpdateVip(GsPosVip vipInfo)
        {
            //ReturnModel returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.VipRegister}", vipInfo.ToJsonString(), 60, "", AppConstVars.sysUser);

            GsEditVipInfo gsEditVipInfo = vipInfo.ToJsonString().JsonToEntity<GsEditVipInfo>();
            GsEditVipCard gsEditVipCardInfo = vipInfo.ToJsonString().JsonToEntity<GsEditVipCard>();

            List<GsEditVipInfo> vipList = new List<GsEditVipInfo>
            {
                gsEditVipInfo
            };
            List<GsEditVipCard> cardList = new List<GsEditVipCard>
            {
                gsEditVipCardInfo
            };

            GsEditVipModel gsEditVipModel = new GsEditVipModel();
            gsEditVipModel.vipList = vipList;
            gsEditVipModel.cardList = cardList;
            string postJson = gsEditVipModel.ToJsonString();
            ReturnModel returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.VipUpdateInfo}", postJson, 60, "", AppConstVars.sysUser);
            return returnModel;
        }

        /// <summary>
        /// 会员发卡
        /// </summary>
        /// <returns></returns>
        public static async Task<ReturnModel> GiveGenerateCard(VipSendCardDto info)
        {
            try
            {
                ReturnModel returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.SendGenerateCard}", JsonConvert.SerializeObject(info), 60, "", AppConstVars.sysUser);

                if (returnModel.Status != ReturnStatus.Success)
                {
                    UIMessageBox.Show($"访问服务器失败,{returnModel.MessageOrString}");
                    return null;
                }
                var res = JsonConvert.DeserializeObject<AdminUiCallBack>(returnModel.MessageOrString);
                if (res.code != 200)
                {
                    UIMessageBox.Show($"会员发卡失败,{res.msg}");
                    return null;
                }
                return returnModel;
            }
            catch (Exception ex)
            {
                UIMessageBox.Show($"会员发卡失败,{ex.Message}");
                return null;
            }
        }


        /// <summary>
        /// 注册并发卡
        /// </summary>
        /// <returns></returns>
        public static async Task<AdminUiCallBack> GiveGenerateCardNew(dynamic info)
        {
            AdminUiCallBack res = new AdminUiCallBack();
            try
            {
                //var url = $"{baseUrl}{SERVER.Member}{API.VipRegistration}";
                //string retStr = Library.HttpHelper.httpPost(url, JsonConvert.SerializeObject(info), _token);
                ReturnModel returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.VipRegistration}", JsonConvert.SerializeObject(info), 60, "", AppConstVars.sysUser);

                if (returnModel.Status != ReturnStatus.Success)
                {
                    UIMessageBox.Show($"访问服务器失败,{returnModel.MessageOrString}");
                    return null;
                }
                res = JsonConvert.DeserializeObject<AdminUiCallBack>(returnModel.MessageOrString);
                if (res.code != 200)
                {
                    UIMessageBox.Show($"注册会员失败,{res.msg}");
                    return null;
                }
            }
            catch (Exception ex) { res.code = 1; res.msg = ex.Message; }
            return res;
        }


        /// <summary>
        /// 注册会员
        /// </summary>
        /// <param name="vipInfo"></param>
        /// <returns></returns>
        public static async Task<AdminUiCallBack> RegisterVip(VipGenerateCardDto info)
        {
            AdminUiCallBack res = new AdminUiCallBack();
            try
            {
                ReturnModel returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.VipRegister}", JsonConvert.SerializeObject(info), 60, "", AppConstVars.sysUser);
                if (returnModel.Status != ReturnStatus.Success)
                {
                    UIMessageBox.Show($"获取会员列表失败,{returnModel.MessageOrString}");
                    return null;
                }
                res = JsonConvert.DeserializeObject<AdminUiCallBack>(returnModel.MessageOrString);
                if (res.code != 200)
                {
                    UIMessageBox.Show($"注册会员失败,{res.msg}");
                    return null;
                }
            }
            catch (Exception ex) { res.code = 1; res.msg = ex.Message; }
            return res;
        }

        /// <summary>
        /// 获取会员卡类型
        /// </summary>
        /// <returns></returns>
        public static async Task<AdminUiCallBack> GetVipCardType()
        {
            AdminUiCallBack res = new AdminUiCallBack();
            try
            {                
                ReturnModel returnModel = await HttpHelperAsync.GetDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.GetCardTypeList}", 60, AppConstVars.sysUser);
                res = JsonConvert.DeserializeObject<AdminUiCallBack>(returnModel.MessageOrString);
                return res;

            }
            catch (Exception ex) { res.code = 1; res.msg = ex.Message; }
            return res;
        }

        /// <summary>
        /// 获取会员储值卡信息
        /// </summary>
        /// <returns></returns>
        public static async Task<VipRechargeCardModel> GetCardByCardNo(string cardNo)
        {
            try
            {
                ReturnModel returnModel = await HttpHelperAsync.GetDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.GetCardByCardNo}/{cardNo}", 60, AppConstVars.sysUser);
                if (!returnModel.Status.Equals(ReturnStatus.Success))
                {
                    UIMessageBox.Show($"请稍后再试");
                    return null;
                }
                ApiReturnModel vipMode = returnModel.MessageOrString.JsonToEntity<ApiReturnModel>();

                if (vipMode.code != 200)
                {
                    UIMessageBox.Show($"检测失败：{vipMode.msg}");
                    return null;
                }
                if (vipMode != null && vipMode.data != null)
                {
                    VipRechargeCardModel apiReturn = vipMode.data.ToString().JsonToEntity<VipRechargeCardModel>();
                    if (apiReturn != null) return apiReturn;
                }
                UIMessageBox.Show($"检测失败：{vipMode.msg}");
            }
            catch (Exception ex) {  }
            return null;
        }

        /// <summary>
        /// 获取会员卡类型
        /// </summary>
        /// <returns></returns>
        public static async Task<AdminUiCallBack> GetVipLevelAsync()
        {
            AdminUiCallBack res = new AdminUiCallBack();
            try
            {
                ReturnModel returnModel = await HttpHelperAsync.GetDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.GetLevelList}", 60, AppConstVars.sysUser);
                res = JsonConvert.DeserializeObject<AdminUiCallBack>(returnModel.MessageOrString);
                return res;
            }
            catch (Exception ex) { res.code = 1; res.msg = ex.Message; }
            return res;
        }
                

        /// <summary>
        /// 会员列表
        /// </summary>
        /// <param name="balance"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static async Task<MemberListResult> GetMemberPage(string sNumber, int pageIndex, int pageSize)
        {
            MemberListResult result = new MemberListResult();
            var memberList = new List<corecmsuser>();
            JObject keyValues = new JObject();
            keyValues.Add(new JProperty("pageNum", pageIndex));
            keyValues.Add(new JProperty("pageSize", pageSize));
            keyValues.Add(new JProperty("beginDate", ""));
            keyValues.Add(new JProperty("endDate", ""));
            keyValues.Add(new JProperty("remark", ""));
            keyValues.Add(new JProperty("vipName", ""));
            keyValues.Add(new JProperty("vipNo", ""));
            keyValues.Add(new JProperty("keywords", sNumber));
            keyValues.Add(new JProperty("vipTel", ""));
            keyValues.Add(new JProperty("cardNo", ""));//储值卡号
            
            try
            {
                ReturnModel returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Member}{API.GetVipList}", keyValues.ToJsonString(), 60,"", AppConstVars.sysUser);
                ApiVipInfoModel resultModel = returnModel.MessageOrString.JsonToEntity<ApiVipInfoModel>();
                if ((resultModel == null) || (resultModel.rows == null)) return null;
                if (resultModel.code == 200)
                {
                    foreach (var vipInfo in resultModel.rows)
                    {
                        memberList.Add(ModelHelper.VipinfoToMember(vipInfo, AppConstVars.sysStore));
                    }
                    result.total = resultModel.total;
                    result.data = memberList;
                    return result;
                }
            }
            catch (Exception ex) { }

            return result;
        }


        public static async Task<corecmsuser> GetListVipInfo(string sNumber)
        {
            var list = await GetListVipList(sNumber);
            if (list == null) return null;
            if (list.Count == 0) return null;
            if (list.Count == 1) return list[0];
            var frm = new FrmUserSelect(list);
            corecmsuser memberSelect = null;
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                memberSelect = frm.memberSelect;
            }
            frm.Dispose();
            return memberSelect;
        }

        public static async Task<corecmsuser> GetListVipInfoById(long vipId)
        {
            try
            {
                var list = await GetListVipList("", vipId);
                if (list == null) return null;
                if (list.Count == 0) return null;
                if (list.Count == 1) return list[0];
                var frm = new FrmUserSelect(list);
                corecmsuser memberSelect = null;
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    memberSelect = frm.memberSelect;
                }
                frm.Dispose();
                return memberSelect;
            }
            catch (Exception ex) { }
            return null;
        }

        /// <summary>
        /// 柚子
        /// </summary>
        /// <param name="sNumber"></param>
        /// <returns></returns>
        public static async Task<List<corecmsuser>> GetListVipList(string sNumber,long vipId = 0)
        {
            try
            {
                //string para = string.Concat($"{SERVER.Member}{API.GetVipInfo}", "/", sNumber);
                string strPara = string.Format("?vipTel={0}&vipId={1}", sNumber, vipId);
                string para = string.Concat($"{SERVER.Member}{API.GetVipInfo}", "/", strPara);
                ReturnModel returnModel = await HttpHelperAsync.GetDataAsync(ApiConsVars.baseUrl, para, 60, AppConstVars.sysUser);

                if (returnModel.Status != ReturnStatus.Success)
                {
                    UIMessageBox.Show($"获取会员列表失败,{sNumber}{returnModel.MessageOrString}");
                    return null;
                }
                AdminUiCallBack res = JsonConvert.DeserializeObject<AdminUiCallBack>(returnModel.MessageOrString);
                if (res.code != 200)
                {
                    UIMessageBox.Show($"获取会员列表失败,{sNumber}{res.msg}");
                    return null;
                }
                List<GsPosVip> vipList = JsonConvert.DeserializeObject<List<GsPosVip>>(res.data.ToString());

                if ((vipList == null) || (vipList.Count == 0))
                {
                    UIMessageBox.Show($"获取会员列表失败,{returnModel.MessageOrString}");
                    return null;
                }

                var memberList = new List<corecmsuser>();
                foreach (var vipInfo in vipList)
                {
                    var member = ModelHelper.VipToMember(vipInfo, AppConstVars.sysStore);
                    memberList.Add(member);
                }
                SaveMemberList(memberList);
                return memberList;
            }
            catch (Exception e)
            {
                UIMessageBox.Show(e.Message, "错误:", UIStyle.Green, UIMessageBoxButtons.OK);
            }
            return null;
        }

        public static void SaveMemberList(List<corecmsuser> memberList)
        {
            try
            {
                new corecmsuserServices().InsertOrUpdateAsync(memberList);
            }
            catch (Exception e)
            {
            }
        }

        /// <summary>
        /// 加载会员优惠券列表
        /// </summary>
        public static async Task<List<VipCouponInfoModel>> GetCouponList(string vipInfoId, sysuser sysuser)
        {
            string para = $"{SERVER.Coupon}{API.GetCouponVip}?vipInfoId={vipInfoId}&branchId={sysuser.corpId}&couponStatus=1";
            ReturnModel returnModel = await HttpHelperAsync.GetDataAsync(ApiConsVars.baseUrl, para, 60, sysuser);

            if (returnModel.Status != ReturnStatus.Success)
            {
                UIMessageBox.Show($"获取券列表失败,{returnModel.MessageOrString}");
                return null;
            }

            VipCouponInfoListModel vipCouponInfoList = returnModel.MessageOrString.JsonToEntity<VipCouponInfoListModel>();

            if (vipCouponInfoList.code != 200)
            {
                UIMessageBox.Show($"获取券列表失败,{vipCouponInfoList.msg}");
                return null;
            }

            List<VipCouponInfoModel> vipCouponInfos = vipCouponInfoList.data.Where(p => p.couponChannelId != "2" && p.couponStatus == "1").ToList();
            return vipCouponInfos;
        }


        /// <summary>
        /// 得到积分兑换商品列表
        /// </summary>
        public static async Task<List<CreditsExchangeModel>> GetIntegralGoodsList(sysuser sysuser)
        {
            string para = $"{SERVER.Product}{API.GetIntegralGoodsList}/0";
            ReturnModel returnModel = await HttpHelperAsync.GetDataAsync(ApiConsVars.baseUrl, para, 60, sysuser);

            if (returnModel.Status != ReturnStatus.Success)
            {
                UIMessageBox.Show($"获取积分兑换商品列表失败,{returnModel.MessageOrString}");
                return null;
            }

            CreditsExchangeListModel redeemGoodsList = returnModel.MessageOrString.JsonToEntity<CreditsExchangeListModel>();
            if (redeemGoodsList == null || redeemGoodsList.code != 200)
            {
                UIMessageBox.Show($"获取积分兑换商品列表失败,{redeemGoodsList.msg}");
                return null;
            }

            return redeemGoodsList.data;
        }

        /// <summary>
        /// 积分兑换
        /// </summary>
        public static async Task<AsyncReturnModel> PostIntegralExchange(corecmsuser member,string ProductId)
        {
            AsyncReturnModel asyncReturnModel = new AsyncReturnModel();
            string para = $"{SERVER.Product}{API.SetIntegralExchange}/?vipId={member.id}&itemId={ProductId}&branchId={AppConstVars.sysStore.branchId}";
            ReturnModel returnModel = await HttpHelperAsync.GetDataAsync(ApiConsVars.baseUrl, para, 60, AppConstVars.sysUser);

            if (returnModel.Status != ReturnStatus.Success)
            {
                UIMessageBox.Show($"兑换错误,{returnModel.MessageOrString}");
                return null;
            }

            ApiReturnModel InfoReturn = returnModel.MessageOrString.JsonToEntity<ApiReturnModel>();
            if (InfoReturn == null || InfoReturn.code != 200)
            {
                UIMessageBox.Show($"兑换错误,{InfoReturn.msg}");
                return asyncReturnModel;
            }
            asyncReturnModel.msg = "兑换成功！";
            asyncReturnModel.status = true;
            return asyncReturnModel;
        }

    }
}
